package com.dbsop.domain.rest;

import com.dbsop.common.enums.DecideFlag;
import com.dbsop.common.enums.Order;
import com.dbsop.common.util.FieldUtils;
import com.dbsop.domain.*;
import com.dbsop.domain.rest.query.*;
import com.dbsop.domain.rest.rank.RestRanking;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * Es条件查询
 */
public class EsRestCondition extends EsCondition {

    /**
     * 结果集排序
     * @param fields
     * @param order
     * @return
     */
    public EsRestCondition queryOrder(Order order, String... fields){
        esOrders.add(new EsOrder(Arrays.stream(fields).collect(Collectors.toList()),order));
        return this;
    }
    /**
     * 于等于
     * @param field
     * @param value
     * @return
     */
    public EsConditionGtRange lte(String field, Object value){
        RestLtRangeQuery lteRangeQuery = new RestLtRangeQuery(field, value, DecideFlag.LTE);
        conditions.add(lteRangeQuery);
        EsRestConditionGtRange esConditionGtRange = new EsRestConditionGtRange(this,lteRangeQuery);
        return esConditionGtRange;
    }
    /**
     * 小于
     * @param field
     * @param value
     * @return
     */
    public EsConditionGtRange lt(String field, Object value){
        RestLtRangeQuery lteRangeQuery = new RestLtRangeQuery(field, value, DecideFlag.LT);
        conditions.add(lteRangeQuery);
        EsRestConditionGtRange esConditionGtRange = new EsRestConditionGtRange(this,lteRangeQuery);
        return esConditionGtRange;
    }

    /**
     * 大于等于
     * @param field
     * @param value
     * @return
     */
    public EsConditionLtRange gte(String field, Object value){
        RestGtRangeQuery gtRangeQuery = new RestGtRangeQuery(field, value, DecideFlag.GTE);
        conditions.add(gtRangeQuery);
        return new EsRestConditionLtRange(this,gtRangeQuery);
    }


    /**
     * 大于
     * @param field
     * @param value
     * @return
     */
    public EsConditionLtRange gt(String field, Object value){
        RestGtRangeQuery gtRangeQuery = new RestGtRangeQuery(field, value, DecideFlag.GT);
        conditions.add(gtRangeQuery);
        return new EsRestConditionLtRange(this,gtRangeQuery);
    }

    /**
     * 于等于
     * @param field
     * @param value
     * @return
     */
    public EsCondition LTE(String field, Object value){
        conditions.add(new RestLtRangeQuery(field, value, DecideFlag.LTE));
        return this;
    }
    /**
     * 小于
     * @param field
     * @param value
     * @return
     */
    public EsCondition LT(String field, Object value){
        RestLtRangeQuery lteRangeQuery = new RestLtRangeQuery(field, value, DecideFlag.LT);
        conditions.add(lteRangeQuery);
        return this;
    }

    /**
     * 大于等于
     * @param field
     * @param value
     * @return
     */
    public EsCondition GTE(String field, Object value){
        conditions.add(new RestGtRangeQuery(field, value, DecideFlag.GTE));
        return this;
    }


    /**
     * 大于
     * @param field
     * @param value
     * @return
     */
    public EsCondition GT(String field, Object value){
        conditions.add( new RestGtRangeQuery(field, value, DecideFlag.GT));
        return this;
    }

    /**
     * 相等
     * @param field
     * @param value
     * @return
     */
    public EsCondition eq(String field, Object value){
        conditions.add(new RestEqQuery(field,value));
        return this;
    }



    public EsCondition like(String field, String value){
        conditions.add(new RestLikeQuery(field,value));
        return this;
    }

    /**
     *  IN查询
     * @param field
     * @return
     */
    public EsCondition in(String field, Object... values){
        RestInQuery inQuery = new RestInQuery(field);
        inQuery.add(values);
        conditions.add(inQuery);
        return this;
    }

    /**
     *  IN查询
     * @param field
     * @return
     */
    public EsCondition in(String field, Collection values){
        RestInQuery inQuery = new RestInQuery(field);
        inQuery.add(values.toArray());
        conditions.add(inQuery);
        return this;
    }

    public EsCondition ranking(String groupField, String  sortField, Order order, int size){
        this.ranking = new RestRanking(groupField, new RankBean(groupField, new EsOrder(sortField, order), size));
        return this;
    }

    /**
     * 分词检索
     * @param field
     * @param values
     * @return
     */
    public EsCondition analyze(String field, String analyze, String... values){
        RestAnalyzeQuery analyzeQuery = new RestAnalyzeQuery(field,analyze);
        analyzeQuery.add(values);
        conditions.add(analyzeQuery);
        return this;
    }

    /**
     * 分词检索
     * @param field
     * @param values
     * @return
     */
    public EsCondition analyze(String field, String analyze, Collection<String> values){
        RestAnalyzeQuery analyzeQuery = new RestAnalyzeQuery(field,analyze);
        analyzeQuery.add(FieldUtils.converList(values));
        conditions.add(analyzeQuery);
        return this;
    }





}
